﻿using HartConfigTool.SerialPortUtil;
using HartConfigTool.UI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace HartConfigTool.HARTCommands.CMDs
{
    internal class CMD3ReadDynamicVarAndLoopCurrent : CMDsBase
    {
        protected CMD3ReadDyncVarsAndLoopCurCodeStruct CMD3ReadDyncVarsAndLoopCurCode;
        public Dictionary<string, IObserver> CMD3Observers;

        [StructLayout(LayoutKind.Sequential, Pack = 1)]
        public struct CMD3ReadDyncVarsAndLoopCurCodeStruct
        {
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
            public byte[] preamble;
            public byte start;
            public byte addressByte1;
            public byte addressByte2;
            public byte addressByte3;
            public byte addressByte4;
            public byte addressByte5;
            public byte cmdID;
            public byte size;
            public byte parity;
        }
        public CMD3ReadDynamicVarAndLoopCurrent(SerialPortsManager serialPortManager)
        {
            ConfigurationParameters(serialPortManager);

            CMD3ReadDyncVarsAndLoopCurCode = new CMD3ReadDyncVarsAndLoopCurCodeStruct()
            {
                preamble = new byte[5] { 0xff, 0xff, 0xff, 0xff, 0xff },
                start = 0x82,
                addressByte1 = 0xA2,
                addressByte2 = 0x82,
                addressByte3 = 0x74,
                addressByte4 = 0x2E,
                addressByte5 = 0x96,
                cmdID = 0x03,
                size = 0x00,
                parity = 0xFF,
            };

            CMD3Observers = new Dictionary<string, IObserver>();
        }
        protected override async Task SendCMDAsync()
        {
            await SendCMDCode();
        }
        protected virtual async Task SendCMDCode()
        {
            await serialPortManager.SendCMD(StructToBytes<CMD3ReadDyncVarsAndLoopCurCodeStruct>(CMD3ReadDyncVarsAndLoopCurCode));
        }
        public override async Task ParseResponse(byte[] responseData)
        {
            await Task.Run(() => { });
        }
    }
}
